package com.filter;
 import java.io.IOException;  
 import java.io.PrintWriter;  
   
 import javax.servlet.Filter;  
 import javax.servlet.FilterChain;  
 import javax.servlet.FilterConfig;  
 import javax.servlet.ServletException;  
 import javax.servlet.ServletRequest;  
 import javax.servlet.ServletResponse;  
 import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;

import com.listener.OnlineUserListener;

   
 public class LoginFilter  implements Filter {  
     private FilterConfig filterConfig;  
   
     // Handle the passed-in FilterConfig  
     public void init(FilterConfig filterConfig) throws ServletException {  
         this.filterConfig = filterConfig;  
     }  
   
     // Process the request/response pair  
     public void doFilter(ServletRequest request, ServletResponse response,  
             FilterChain filterChain)throws IOException, ServletException {  
             HttpServletRequest httpRequest = (HttpServletRequest) request;  
             HttpServletResponse httpResponse = (HttpServletResponse) response;
             request.setCharacterEncoding("utf-8");
             HttpSession hs=httpRequest.getSession();
             Object dlyh=hs.getAttribute("dlyh");
             PrintWriter out;
				if(httpRequest.getRequestURI()!=null&&!httpRequest.getRequestURI().equals("/index.jsp")&&!httpRequest.getRequestURI().contains("login.do")){
					if(dlyh==null||dlyh.equals("")){
						hs.invalidate();
						out = httpResponse.getWriter();
			              out.write("<script>alert('登陆无效，请重新登陆');window.parent.location.href='/index.jsp'</script>");
			              out.close();
		             }
					else{
						filterChain.doFilter(request, response);
						}
				}
				else{
				filterChain.doFilter(request, response);
				}
     }  
   
     // Clean up resources  
     public void destroy() {  
     }  
 }  